package com.tivo.jipviewer;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class BundleRowTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 5578118532127300136L;

	public static final int BUNDLE_SYMBOLIC_NAME = 0;
	public static final int BUNDLE_NAME = 1;
	public static final int BUNDLE_VERSION = 2;
	public static final int BUNDLE_DESCRIPTION = 3;
	public static final int BUNDLE_LICENSE= 4;
	public static final int BUNDLE_VENDOR = 5;
	public static final int BUDNLE_MANIFEST_VERSION = 6;
	public static final int IMPORT_PACKAGE = 7;
	public static final int EXPORT_PACKAGE = 8;
	public static final int PRIVATE_PACKAGE = 9;

	public static final int SV_WIDTH[] = {50, 100, 25, 50, 50, 190, 25, 200, 200, 200};

	List<JipBundle> bundleRow = new ArrayList<JipBundle>();

	public void add(JipBundle bundle){

		bundleRow.add(bundle);
		int iLast = bundleRow.size();
        fireTableRowsInserted(iLast, iLast);
	}

	public void clear() {
        int iLast = bundleRow.size();
        bundleRow.clear();
        fireTableRowsDeleted(0, iLast);
    }

	public JipBundle getRow(int i){
		return bundleRow.get(i);
	}

	public int getRowCount() {
        return bundleRow.size();
    }

    public int getColumnCount() {
        return 9;
    }

    public String getColumnName(int iColumn) {
        switch (iColumn) {
          case BUNDLE_SYMBOLIC_NAME:	 return JipBundle.BUNDLE_SYMBOLIC_NAME;
          case BUNDLE_NAME:  			 return JipBundle.BUNDLE_NAME;
          case BUNDLE_VERSION:        	 return JipBundle.BUNDLE_VERSION;
          case BUNDLE_DESCRIPTION:   	 return JipBundle.BUNDLE_DESCRIPTION;
          case BUNDLE_LICENSE:      	 return JipBundle.BUNDLE_LICENSE;
          case BUNDLE_VENDOR: 			 return JipBundle.BUNDLE_VENDOR;
          case BUDNLE_MANIFEST_VERSION:  return JipBundle.BUNDLE_MANIFEST_VERSION;
          case IMPORT_PACKAGE: 			 return JipBundle.IMPORT_PACKAGE;
          case EXPORT_PACKAGE:			 return JipBundle.EXPORT_PACKAGE;
          case PRIVATE_PACKAGE :		 return JipBundle.PRIVATE_PACKAGE;
          default:       throw new RuntimeException("bad column index");
        }
    }

    @SuppressWarnings("unchecked")
	public Class getColumnClass(int iColumn){
    	return String.class;
    }


	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {

		JipBundle bundle = bundleRow.get(rowIndex);

		switch (columnIndex) {
        case BUNDLE_SYMBOLIC_NAME:	 return splitLines(bundle.getBundleSymbolicName(), columnIndex)[0];
        case BUNDLE_NAME:  			 return splitLines(bundle.getBundleName(), columnIndex)[0];
        case BUNDLE_VERSION:         return splitLines(bundle.getBundleVersion(), columnIndex)[0];
        case BUNDLE_DESCRIPTION:   	 return splitLines(bundle.getBundleDescription(), columnIndex)[0];
        case BUNDLE_LICENSE:      	 return splitLines(bundle.getBundleLicense(), columnIndex)[0];
        case BUNDLE_VENDOR: 		 return splitLines(bundle.getBundleVendor(), columnIndex)[0];
        case BUDNLE_MANIFEST_VERSION:  return splitLines(bundle.getBundleManifestVersion(), columnIndex)[0];
        case IMPORT_PACKAGE: 		 return splitLines(bundle.getImportPackage(), columnIndex)[0];
        case EXPORT_PACKAGE:		 return splitLines(bundle.getExportPackage(), columnIndex)[0];
        case PRIVATE_PACKAGE :		 return splitLines(bundle.getPrivatePackage(), columnIndex)[0];
        default:       throw new RuntimeException("bad column index");
      }
	}

	private String[] splitLines(String columnText, int columnIndex){

		return new String[]{columnText};

//		List<String> columnLines = new ArrayList<String>();
//
//		if (columnText == null)
//			return new String[]{""};
//
//		while (columnText.length() > new Integer(SV_WIDTH[columnIndex] / 5)){
//
//			columnLines.add(columnText.substring(0, new Integer(SV_WIDTH[columnIndex] / 5)));
//			columnText = columnText.substring(new Integer(SV_WIDTH[columnIndex] / 5)+1, columnText.length());
//		}
//		columnLines.add(columnText);
//
//		String[] value = columnLines.toArray(new String[columnLines.size()]);
//		return value;
	}
}


